package Test;

import DB.StudentDB;
import DB.TeamDB;
import Model.Student;
import Model.Team;
import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.fail;

public class StudentDBTest {

    //OK!
    @Test
    public void testInsertStudent() throws Exception {
        StudentDB studentDB = new StudentDB();
        TeamDB teamDB = new TeamDB();
        Team team = teamDB.findTeam(2);

        int id = studentDB.studentGetMax();

        Student student = new Student();
        student.setId(id);
        student.setName("Emil Bolet");
        student.setUsername("lime");
        student.setPassword("emilforprez");
        student.setAddress("Sigrid");
        student.setEmail("asd@asd.dk");
        student.setPhoneNo(88888888);
        student.setType("Student");
        student.setStudentId(id);
        student.setStudentNo(147147147);
        student.setTeamId(team);

        int rc = studentDB.insertStudent(student);

        if (rc == 1) {
            System.out.println("Inserting student/person successful");
        }
        else {
            fail("Failed inserting student/person");
        }
    }

    //OK!!
    @Test
    public void testFindStudentByID() throws Exception {
        StudentDB studentDB = new StudentDB();
        int find = 2;

        Student student = studentDB.findStudentByID(find);

        if (student != null) {
            System.out.println(student.getId());
            System.out.println(student.getName());
            System.out.println(student.getUsername());
            System.out.println(student.getPassword());
            System.out.println(student.getAddress());
            System.out.println(student.getEmail());
            System.out.println(student.getPhoneNo());
            System.out.println(student.getType());
            System.out.println(student.getStudentId());
            System.out.println(student.getStudentNo());
            System.out.println(student.getTeamId().getName());
        }
        else {
            fail("Failed finding student - student is null");
        }
    }


    //OK!!!
    @Test
    public void testFindStudent() throws Exception {
        StudentDB studentDB = new StudentDB();
        String userName = "1026895";
        String password=  "kodeord";

        Student student = studentDB.findStudent(userName, password);

        if (student != null) {
            System.out.println(student.getId());
            System.out.println(student.getName());
            System.out.println(student.getUsername());
            System.out.println(student.getPassword());
            System.out.println(student.getAddress());
            System.out.println(student.getEmail());
            System.out.println(student.getPhoneNo());
            System.out.println(student.getType());
            System.out.println(student.getStudentId());
            System.out.println(student.getStudentNo());
            System.out.println(student.getTeamId().getName());
        }
        else {
            fail("Failed finding student - student is null");
        }
    }

    //OK!!!!
    @Test
    public void testFindStudentByStudentNo()
    {
        StudentDB studentDB = new StudentDB();
        int studentNo = 147147147;

        Student student = studentDB.findStudentByStudentNo(studentNo);

        if (student != null) {
            System.out.println(student.getId());
            System.out.println(student.getName());
            System.out.println(student.getUsername());
            System.out.println(student.getPassword());
            System.out.println(student.getAddress());
            System.out.println(student.getEmail());
            System.out.println(student.getPhoneNo());
            System.out.println(student.getType());
            System.out.println(student.getStudentId());
            System.out.println(student.getStudentNo());
            System.out.println(student.getTeamId().getName());
        }
        else {
            fail("Failed finding student - student is null");
        }
    }
    //OK!!!!!
    @Test
    public void testGetAllStudents() throws Exception {
        StudentDB studentDB = new StudentDB();
        ArrayList<Student> studentObj = studentDB.getAllStudents(false);

        if (studentObj != null) {
            for (Student s : studentObj) {
                System.out.println(s.getId() + " " + s.getName() + " " + s.getStudentId());
            }
        }
        else {
            fail("No students found");
        }
    }
    //OK!!!!!!
    @Test
    public void testGetStudentsByTeam()
    {
        StudentDB studentDB = new StudentDB();
        TeamDB teamDB = new TeamDB();

        ArrayList<Student> students = studentDB.getStudentsByTeam(teamDB.findTeam(1), false);

        if (students != null) {
            for (Student s : students) {
                System.out.println(s.getId() + " " + s.getName() + " " + s.getStudentId());
            }
        }
        else {
            fail("No students found");
        }


    }
    //OK!!!!!!
    @Test
    public void testUpdateStudent() throws Exception {
        StudentDB studentDB = new StudentDB();
        TeamDB teamDB = new TeamDB();
        Team team = teamDB.findTeam(2);
        String updateValue = "Emil Bolet";

        Student student = new Student();
        student.setId(2);
        student.setName(updateValue);
        student.setUsername("lime");
        student.setPassword("emilforprez");
        student.setAddress("Sigrid");
        student.setEmail("asd@asd.dk");
        student.setPhoneNo(88888888);
        student.setType("Student");
        student.setStudentId(2);
        student.setStudentNo(147147146);
        student.setTeamId(team);

        studentDB.updateStudent(student);

        String studentName = studentDB.findStudentByID(2).getName();

        if (studentName.equals(updateValue)) {
            System.out.println("Update successful");
        }
        else {
            fail("Update failed");
        }

    }
    //OKAY!!!
    @Test
    public void testDeleteStudent() throws Exception {
        StudentDB studentDB = new StudentDB();
        TeamDB teamDB = new TeamDB();
        Team team = teamDB.findTeam(1);
        Student student = new Student(1000, "Delete", "Delete", "Delete", "Delete", "Delete", 123123123, "Delete", 1000, 1000, team);
        studentDB.insertStudent(student);

        if (studentDB.findStudentByID(1000) != null) {

            studentDB.deleteStudent(1000);

            if (studentDB.findStudentByID(1000) == null) {
                System.out.println("Delete successful");
            }
            else {
                fail("Delete not successful");
            }
        }
        else {
            fail("Inserting for deletion failed");
        }
    }
}
